package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.UserManageMapper;
import com.example.mybatisplus.model.domain.UserManage;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.UserManageService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.WhitelistSettingService;
import com.example.mybatisplus.model.domain.WhitelistSetting;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author sharplu
 * @since 2023-07-06
 * @version v1.0
 */
@Controller

@RequestMapping("/whitelistSetting")
public class WhitelistSettingController {

    private final Logger logger = LoggerFactory.getLogger( WhitelistSettingController.class );

    @Autowired
    private WhitelistSettingService whitelistSettingService;

    @Autowired
    private UserManageMapper userManageMapper;

    @PostMapping("login")
    @ResponseBody
    public JsonResponse login(WhitelistSetting whitelistSetting){
        // login 为空  没有这个匹配的
       WhitelistSetting login = whitelistSettingService.login(whitelistSetting);
       // 把登录用户存进session
        SessionUtils.saveCurUser(login);
       return JsonResponse.success(login);
    }

    @GetMapping("list")
    @ResponseBody
    public JsonResponse list(PageDTO pageDTO,WhitelistSetting whitelistSetting){
        Page<WhitelistSetting> page= whitelistSettingService.pageList(pageDTO,whitelistSetting);
        System.out.println(111);
        return JsonResponse.success(page);
    }

    @PostMapping("update")
    @ResponseBody
    public JsonResponse update(WhitelistSetting whitelistSetting){
        return JsonResponse.success(whitelistSettingService.updateById(whitelistSetting));
    }

    @GetMapping("removeById")
    @ResponseBody
    public JsonResponse removeById(Long id){
        boolean b = whitelistSettingService.removeById(id);
        return JsonResponse.success(b);
    }

    @GetMapping("logout")
    @ResponseBody
    public JsonResponse logout(){
        SessionUtils.removeCurUser();
        return JsonResponse.success(true);
    }

    @PostMapping("insert")
    @ResponseBody
    public  JsonResponse insert(String sn){
        UserManage userManage = userManageMapper.selectByUserSn(sn);
        System.out.println(userManage.toString());
        if (userManage != null){
            WhitelistSetting whitelistSetting1 = new WhitelistSetting(userManage.getUserSn(),userManage.getUserName(),userManage.getRoleId());
            whitelistSettingService.save(whitelistSetting1);
            return JsonResponse.success(true);
        }
        return JsonResponse.success(false);
    }


}

